<?php

define ('swf_tag_identify', 		 "FWS");
define ('swf_tag_compressed', 		 "CWS");

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class FlashComponent extends Component {
    public function __construct() {
    }

    
    public function readCreativeDetails($file_path){
        $data       =   array();
        $content    =   @file_get_contents($file_path);
        list($data['width'], $data['height']) = $this->phpAds_SWFDimensions($content);
        $data['pluginVersion']  = $this->phpAds_SWFVersion($content);
        unset ($content);
        return $data;
    }
    /*-------------------------------------------------------*/
    /* Get the Flash version of the banner                   */
    /*-------------------------------------------------------*/

    public function phpAds_SWFVersion($buffer)
    {
            if (substr($buffer, 0, 3) == swf_tag_identify ||
                    substr($buffer, 0, 3) == swf_tag_compressed)
                    return ord(substr($buffer, 3, 1));
            else
                    return false;
    }
    /*-------------------------------------------------------*/
    /* Is the Flash file compressed?                         */
    /*-------------------------------------------------------*/

    private function phpAds_SWFCompressed($buffer)
    {
            if (substr($buffer, 0, 3) == swf_tag_compressed)
                    return true;
            else
                    return false;
    }
    /*-------------------------------------------------------*/
    /* Decompress Flash file                                 */
    /*-------------------------------------------------------*/

    private function phpAds_SWFDecompress($buffer)
    {
            if (function_exists('gzuncompress') &&
                    substr($buffer, 0, 3) == swf_tag_compressed &&
                    ord(substr($buffer, 3, 1)) >= 6)
            {
                    $output  = 'F';
                    $output .= substr ($buffer, 1, 7);
                    $output .= gzuncompress (substr ($buffer, 8));

                    return ($output);
            }
            else
                    return ($buffer);
    }
    /*-------------------------------------------------------*/
    /* Compress Flash file                                   */
    /*-------------------------------------------------------*/

    public function phpAds_SWFCompress($buffer)
    {
            $version = ord(substr($buffer, 3, 1));

            if (function_exists('gzcompress') &&
                substr($buffer, 0, 3) == swf_tag_identify &&
                    $version >= 3)
            {
                    // When compressing an old file, update
                    // version, otherwise keep existing version
                    if ($version < 6) $version = 6;

                    $output  = 'C';
                    $output .= substr ($buffer, 1, 2);
                    $output .= chr($version);
                    $output .= substr ($buffer, 4, 4);
                    $output .= gzcompress (substr ($buffer, 8));

                    return ($output);
            }
            else
                    return ($buffer);
    }
    private function phpAds_SWFBits($buffer, $pos, $count)
    {
            $result = 0;

            for ($loop = $pos; $loop < $pos + $count; $loop++)
                    $result = $result + ((((ord($buffer[(int)($loop / 8)])) >> (7 - ($loop % 8))) & 0x01) << ($count - ($loop - $pos) - 1));

            return $result;
    }
    /*-------------------------------------------------------*/
    /* Get the dimensions of the Flash banner                */
    /*-------------------------------------------------------*/
    
    public function phpAds_SWFDimensions($buffer)
    {
            // Decompress if file is a Flash MX compressed file
            if ($this->phpAds_SWFCompressed($buffer))
                    $buffer = $this->phpAds_SWFDecompress($buffer);

            // Get size of rect structure
            $bits   = $this->phpAds_SWFBits ($buffer, 64, 5);

            // Get rect
            $width  = (int)(($this->phpAds_SWFBits ($buffer, 69 + $bits, $bits) - $this->phpAds_SWFBits ($buffer, 69, $bits)) / 20);
            $height = (int)(($this->phpAds_SWFBits ($buffer, 69 + (3 * $bits), $bits) - $this->phpAds_SWFBits ($buffer, 69 + (2 * $bits), $bits)) / 20);


            return (
                    array($width, $height)
            );
    }
}
?>
